using System;
using System.Xml;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Plain.Utilities
{
	public static class XmlExtensions
	{
	public static XmlNode SelectSingleNodeIgnoreNameSpace(this XmlDocument doc, string xpath){
			return doc.SelectSingleNode(XPathIgnoreNamespaces(xpath));
				
	}
	public static IEnumerable<XmlNode> SelectNodesIgnoreNameSpace(this XmlDocument doc, string xpath){	
			return doc.SelectNodes(XPathIgnoreNamespaces( xpath)).Cast<XmlNode>();
				
	}
		public static string XPathIgnoreNamespaces(string xpath){
			if(!xpath.StartsWith("/")){
				xpath= "./" + xpath;
			}
			string newxpath = Regex.Replace(xpath,@"/(?<node>[\w]+)\[","/*[local-name()='${node}' and ");	
			newxpath = Regex.Replace(newxpath,@"/(?<node>[\w]+)","/*[local-name()='${node}']");
			//newxpath = Regex.Replace(newxpath,@"\[(?<node>[\w]+)","[*[local-name()=='${node}']");
			return newxpath;
		}
	}
}

